package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AuthModuleModel;
import com.snake.springboot.authority.api.service.AuthModuleService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthModuleDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthModuleMapper;
import com.snake.springboot.authority.service.assembler.AuthModuleAssembler;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类 {@code AuthModuleServiceImpl}  <br> .
 * <p>详细描述
 * 创建时间：2024/5/27 14:27
 *
 * @author Yzhui
 * @see
 */
@Service
public class AuthModuleServiceImpl implements AuthModuleService {

    private AuthModuleMapper authModuleMapper;

    @Autowired
    public AuthModuleServiceImpl(AuthModuleMapper authModuleMapper) {
        this.authModuleMapper = authModuleMapper;
    }

    @Override
    public AuthModuleModel addModule(AuthModuleModel model) throws Exception {
        AuthModuleDO entity = AuthModuleAssembler.assembleAuthModuleDO(model, true);
        authModuleMapper.insert(entity);
        model.setModuleId(entity.getModuleId());
        return model;
    }

    @Override
    public int modifyModule(AuthModuleModel model) throws Exception {
        AuthModuleDO entity = AuthModuleAssembler.assembleAuthModuleDO(model, false);
        return authModuleMapper.update(entity);
    }

    @Override
    public int removeModule(String moduleId) {
        return authModuleMapper.delete(moduleId);
    }

    @Override
    public AuthModuleModel querySingleModule(String moduleId) {
        AuthModuleDO entity = authModuleMapper.findById(moduleId);
        AuthModuleModel result = AuthModuleAssembler.assembleAuthModuleModel(entity);
        return result;
    }

    @Override
    public List<AuthModuleModel> queryModuleList(AuthModuleModel params) throws Exception {
        AuthModuleDO condition = AuthModuleAssembler.assembleAuthModuleDO(params, false);
        List<AuthModuleDO> list = authModuleMapper.findByCondition(condition);
        List<AuthModuleModel> result = ListUtils.copy(list, AuthModuleModel.class);
        return result;
    }
}
